<?php

namespace App\Http\Controllers\Invoice\Web;

use App\Http\Controllers\BaseController;
use App\Jobs\Export\Export;
use App\Service\Invoice\Web\InvoiceService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class IndexController extends BaseController
{

    /**
     * 查看开票记录列表
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function indexList(Request $request): JsonResponse
    {
        $data['shopIds']           = (string)$request->get('shopIds', '');                 // 门店id 逗号隔开
        $data['blueInvoiceNumber'] = (string)$request->get('blueInvoiceNumber', '');       // 发票号码
        $data['orderNum']          = (string)$request->get('orderNum', '');                // 订货单号
        $data['invoiceType']       = (string)$request->get('invoiceType', '');             // 发票类型 1普通发票，2专用发票
        $data['status']            = (string)$request->get('status', '');                  // 发票状态 1:已提交待审核 2:审批完成待发票生成 3:发票生成失败 4:发票生成成功
        $data['channel']           = (string)$request->get('channel', '');                 // 开票渠道 1自助开票 2财务开票
        $data['headerType']        = (string)$request->get('headerType', '');              // 抬头类型 0:个人 :企业
        $data['name']              = (string)$request->get('name', '');                    // 申请人名字
        $data['invoiceTitle']      = (string)$request->get('invoiceTitle', '');            // 发票抬头
        $data['startTime']         = (string)$request->get('startTime', '');               // 开票时间
        $data['endTime']           = (string)$request->get('endTime', '');
        $pageSize                  = (int)$request->get('pageSize', 20);
        $pageNum                   = (int)$request->get('pageNum', 1);
        $export                    = (int)$request->get("export", 0);
        if ($export) {
            $pageNum  = 1;
            $pageSize = 9999;
        }
        if ($data['startTime']) {
            $data['startTime'] = strtotime($data['startTime']);
        }
        if ($data['endTime']) {
            $data['endTime'] = strtotime($data['endTime'] . ' 23:59:59');
        }
        //导出异步处理
        if ($export) {
            $currentUid = getRmsUserId();
            dispatch(
                (new Export(
                    '发票记录列表-' . date('Y-m-d') . "-" . time(), Export::TYPE_APPRAISE, [
                        'data'       => [
                            'data'     => $data,
                            'export'   => $export,
                            'pageSize' => $pageSize,
                            'pageNum'  => $pageNum,
                        ],
                        'userId'     => $currentUid,
                        'export'     => $export,
                        'exportType' => Export::TYPE_INVOICE,
                        'startTime'  => $data['startTime'],
                        'endTime'    => $data['endTime'],
                    ]
                ))
            )->onQueue(Export::QUEUE_NAME);
            return $this->success([]);
        }
        return $this->success(InvoiceService::lists($data, $pageSize, $pageNum, $export));
    }

    /**
     * 查看开票记录详情
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function info(Request $request, int $id): JsonResponse
    {
        return $this->success(InvoiceService::info($id));
    }
}
